<template>
    <div class="home-container">
        <!-- 导航栏 -->
        <van-nav-bar class="page-nav-bar" fixed>
            <van-button class="search-btn" slot="title" icon="search" type="info" size="small" to="/search" round>搜索</van-button>
        </van-nav-bar>
        <!-- 频道列表 -->
        <van-tabs class="channel-tabs" v-model="active" animated swipeable>
            <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">
                <ArticleList :channel="channel"></ArticleList>
            </van-tab>
            <div slot="nav-right" class="placeholder"></div>
            <div slot="nav-right" class="hamburger-btn" @click="isChennelEditShow = true">
                <i class="iconfont icon-gengduo"></i>
            </div>
        </van-tabs>
        <!-- 频道编辑 -->
        <van-popup v-model="isChennelEditShow" closeable close-icon-position="top-left" position="bottom" :style="{ height: '100%' }">
            <channel-edit @update-active="upDateActive" :my-channels="channels" :active="active"></channel-edit>
        </van-popup>
    </div>
</template>

<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/article-list";
import ChannelEdit from "./components/channel-edit";
import { mapGetters } from "vuex";
export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    ChannelEdit,
  },
  data() {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false,
    };
  },
  computed: {
    ...mapGetters(["token"]),
  },
  methods: {
    async loadChannels() {
      try {
        let channels = [];
        if (this.token) {
          const { data } = await getUserChannels();
          channels = data.data.channels;
        } else {
          const localChannels = JSON.parse(
            localStorage.getItem("TOUTIAO_CHANNELS")
          );
          if (localChannels) {
            channels = localChannels;
          } else {
            const { data } = await getUserChannels();
            channels = data.data.channels;
          }
        }
        this.channels = channels;
      } catch (error) {
        this.$toast("获取频道数据失败");
      }
    },
    upDateActive(index, isChennelEditShow = true) {
      this.active = index;
      this.isChennelEditShow = isChennelEditShow;
    },
  },
  created() {
    this.loadChannels();
  },
};
</script>

<style lang="scss" scoped>
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      i.icon-gengduo {
        font-size: 33px;
      }
      &::before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background: url("~@/assets/gradient-gray-line.png");
        background-size: contain;
      }
    }
  }
}
</style>